package com.crayon2f.test;

import org.junit.Test;

public class Interview {

    //二分查找
    public static void main(String[] args) {

        int[] array = {1, 2, 3, 4, 6};
        System.out.println(find(array, 3));
    }

    public static int find(int[] array, int ele) {

        int left = 0, right = array.length - 1, midd;
        if (left > right || ele < array[left] || ele > array[right]) {
            return -1;
        }
        while (left <= right) {
            midd = (left + right) / 2;
            if (array[midd] > ele) {
                right = midd - 1;
            } else if (array[midd] < ele) {
                left = midd + 1;
            } else {
                return midd;
            }
        }
        return -1;
    }
}
